package cm.unit3.theme4;

import cm.commom.TreeNode;

public class HasSubtree {

    public boolean equalsOfTree(TreeNode root1, TreeNode root2){
        if ( (root1==null && root2==null)||
                (root1!=null && root2==null) ){
            return true;
        }

        if (root1!=null){
            return (root1.val==root2.val) &&
                    equalsOfTree(root1.left,root2.left) &&
                    equalsOfTree(root1.right,root2.right);
        }
        return false;
    }

    public boolean HasSubtree(TreeNode root1, TreeNode root2) {
        // 遍历
        if (root1!=null && root2!=null){
            if (root1.val==root2.val){
                if (equalsOfTree(root1,root2)){
                    return true;
                }
            }
            return HasSubtree(root1.left,root2) ||
                    HasSubtree(root1.right,root2);
        }
        return false;
    }
}
